package com.jwb.pub.serviceImpl;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.jwb.pub.dao.BaseDAO;
import com.jwb.pub.model.JavaModel;
import com.jwb.pub.model.Page;
import com.jwb.pub.service.UploadFileService;

@Service
public class UploadFileServiceImpl implements UploadFileService{
	@Autowired
	private BaseDAO baseDAO;
	public JSONObject upfileSave(JSONObject jsdata)
	{
		JSONObject jsobject = new JSONObject();
		jsobject.put("respCode", "-1");
		jsobject.put("respDesc", "数据异常");		
		try
      	{      		
			String operType=!jsdata.containsKey("operType") ? "" : jsdata.getString("operType");
			String id=!jsdata.containsKey("id") ? "" : jsdata.getString("id");
			String title=!jsdata.containsKey("title") ? "" : jsdata.getString("title");
			String rmk=!jsdata.containsKey("rmk") ? "" : jsdata.getString("rmk");
			String fileNo=!jsdata.containsKey("fileNo") ? "" : jsdata.getString("fileNo");				
			String sql="";
			String status="0";	//改写成batch
			
			LinkedHashMap<String,ArrayList<JavaModel>> map = new LinkedHashMap<String,ArrayList<JavaModel>>();
			
			if("add".equals(operType))
    		{        			
    			//未发布添加
			
        		sql="insert into upload_file"+
                		"	      (id,"+
                		"			   title,"+              		
                		"			   create_date,"+
                		"			   status,"+
                		"			   file_no,"+                	
                		"			   rmk"+               
                		"			   )"+
                		"			values"+
                		"			  ("+id+","+
                		"			   '"+title+"',"+             
                		"			   NOW(),"+
                		"			   '"+status+"',"+
                		"			   '"+fileNo+"',"+                
                		"			   ?"+               
                		"			   )";
        		
    		}
			else if("edit".equals(operType))
    		{
    			//未发布 修改
    			sql="update upload_file"+
						"   set "+					
						"       title = '"+title+"',"+						
						"       status = '"+status+"',"+
						"       file_no = '"+fileNo+"',"+					
						"       rmk = ?"+					
						" where id = "+id;
    		}
			ArrayList<JavaModel> list = new ArrayList<JavaModel>();
  			list = new ArrayList<JavaModel>();
  			list.add(new JavaModel(rmk));
  			map.put(sql, list); 	
  			boolean flag = baseDAO.excuteBatchOrderedSql(map);  			
  			if(!flag){
  				jsobject.put("respCode", "-1");
  	           	jsobject.put("respDesc", "失败");  
  	           	return jsobject;
  			} 
  			
  			jsobject.put("respCode", "0");      				
  	        jsobject.put("respDesc", "成功");
  			      		
      	}   
      	catch(Exception e)
      	{
      		e.printStackTrace();
      	}
			
	
		
		return jsobject;
	}
	
	
	
	
	public JSONObject upfileInfo(JSONObject jsdata) {
		JSONObject jsobject = new JSONObject();
		try
      	{      		 
      		 String sqlWhere="";
      		 String sqlOrder="";
      		 String   sql="select id,  "+
					"       title,"+
					"       rmk,"+
					"       DATE_FORMAT(create_date, '%Y-%m-%d %H:%i:%s') create_date,"+
					"       status,"+
					"       file_no ,"+		
					"       (SELECT file_path FROM attach_file_detail a WHERE a.file_no=m.file_no AND a.status='a'  AND file_type LIKE '%image%' LIMIT 1)   pic_url, "+		
                    "       (SELECT file_path FROM attach_file_detail a WHERE a.file_no=m.file_no AND a.status='a' AND file_type LIKE '%video%'  LIMIT 1)   video_url "+		
			        "  from upload_file m  where status<>'9' ";
				 String id="";
				 if (jsdata.containsKey("id"))
				 {
					 id=jsdata.getString("id");
					 
				 }			
			    
				if(!"".equals(id))
				{
					sqlWhere+=" and m.id="+id;
				}	
				
			    sqlOrder=" ";
			    sql+=sqlWhere+sqlOrder;	       
			    jsobject=baseDAO.findBySqlToJosn(sql);
      		
      	}   
      	catch(Exception e)
      	{
      		
      	}
      	
		return jsobject;
	}




	@Override
	public JSONObject uplist(JSONObject jsdata, Page page) {
		JSONObject jsobject = new JSONObject();
		 String pageFlag=!jsdata.containsKey("pageFlag") ? "" : jsdata.getString("page_flag");
		 String title=!jsdata.containsKey("title") ? "" : jsdata.getString("title");
		try
     	{      		 
     		    String sqlWhere="";
     		    String sqlOrder="";
     		   if(!"".equals(title))
     		    {
     		    	sqlWhere+=" and title like '%"+title+"%'";
     		    }
     		    String   sql="select id,  "+
   					"       title,"+
   					"       rmk,"+
   					"       DATE_FORMAT(create_date, '%Y-%m-%d %H:%i:%s') create_date,"+
   					"       status,"+
   					"       file_no ,"+		
   					"       (SELECT file_path FROM attach_file_detail a WHERE a.file_no=m.file_no AND a.status='a'  AND file_type LIKE '%image%' LIMIT 1)   pic_url, "+		
                       "       (SELECT file_path FROM attach_file_detail a WHERE a.file_no=m.file_no AND a.status='a' AND file_type LIKE '%video%'  LIMIT 1)   video_url "+		
   			        "  from upload_file m  where status<>'9' order by create_date desc";
			    sql+=sqlWhere+sqlOrder;	  
			    if("".equals(pageFlag))
			    {
			    	jsobject=baseDAO.findBySqlToJosn(sql,page);
			    }
			    else
			    {
			    	jsobject=baseDAO.findBySqlToJosn(sql);
			    }
     	}   
     	catch(Exception e)
     	{
     		
     	}
     	
		return jsobject;
	}
	
	
	
	public JSONObject upfileDel(JSONObject jsdata)
	{
		JSONObject jsobject = new JSONObject();
		jsobject.put("respCode", "-1");
		jsobject.put("respDesc", "数据异常");		
		try
      	{      		
			String id=!jsdata.containsKey("id") ? "" : jsdata.getString("id");
			
			
			LinkedHashMap<String,ArrayList<JavaModel>> map = new LinkedHashMap<String,ArrayList<JavaModel>>();
			String sql="";
			
			
    			
			sql="update upload_file"+
					"   set "+								
					"  status = '9'"+						
					" where id = ?";
    	
			ArrayList<JavaModel> list = new ArrayList<JavaModel>();
  			list = new ArrayList<JavaModel>();
  			list.add(new JavaModel(id));
  			map.put(sql, list); 	
  			boolean flag = baseDAO.excuteBatchOrderedSql(map);  			
  			if(!flag){
  				jsobject.put("respCode", "-1");
  	           	jsobject.put("respDesc", "失败");  
  	           	return jsobject;
  			} 
  			
  			jsobject.put("respCode", "0");      				
  	        jsobject.put("respDesc", "成功");
  			      		
      	}   
      	catch(Exception e)
      	{
      		e.printStackTrace();
      	}
			
	
		
		return jsobject;
	}
}
